package com.lungle.warmdiary.app.ui;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.ProgressBar;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.lungle.warmdiary.R;
import com.lungle.warmdiary.app.utils.TDevice;

/**
 * Created by zhangxueyuan on 2016/10/16.
 */
public class ImagePreview2 {


    private PopupWindow mImagePreviewPop;
    private ProgressBar mPbLoading;
    private Context mContext;
    private LayoutInflater mInflater;
    private ImageView mIvPreview;

    public ImagePreview2(Context context) {
        mContext = context;
        initPicturePreview();
    }

    private void initPicturePreview() {
        if (mImagePreviewPop == null) {
            mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = mInflater.inflate(R.layout.view_image_preview, null);
            mPbLoading = (ProgressBar) view.findViewById(R.id.pb_loading);
            mIvPreview = (ImageView) view.findViewById(R.id.iv_preview);
            mIvPreview.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mImagePreviewPop.dismiss();
                    hidePreview();
                }
            });

            mImagePreviewPop = new PopupWindow(view, (int) TDevice.getScreenWidth(), (int) TDevice.getScreenHeight());
            mImagePreviewPop.setBackgroundDrawable(new BitmapDrawable());
            mImagePreviewPop.update();
            mImagePreviewPop.setFocusable(false);
            mImagePreviewPop.setOutsideTouchable(true);
            //实例化一个ColorDrawable颜色为半透明
            ColorDrawable dw = new ColorDrawable(0xb0000000);
            //设置SelectPicPopupWindow弹出窗体的背景
            mImagePreviewPop.setBackgroundDrawable(dw);
            mImagePreviewPop.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    hidePreview();
                }
            });
//            mSelectActive.setBackgroundDrawable(new BitmapDrawable());
        }
    }




    public void showPreview(View view, String url) {
        mPbLoading.setVisibility(View.VISIBLE);
        mIvPreview.setVisibility(View.INVISIBLE);
        mImagePreviewPop.showAtLocation(view, Gravity.CENTER, 0, 0);
        mPbLoading.setVisibility(View.VISIBLE);
        ImageLoader.getInstance().loadImage(url, new SimpleImageLoadingListener() {
            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                mPbLoading.setVisibility(View.GONE);
                mIvPreview.setVisibility(View.VISIBLE);
                mIvPreview.setImageBitmap(loadedImage);

            }
        });
    }


    public void showPreview(View view, Drawable guideImageValue) {

        mImagePreviewPop.showAtLocation(view, Gravity.CENTER, 0, 0);
        mPbLoading.setVisibility(View.GONE);
        mIvPreview.setVisibility(View.VISIBLE);
        mIvPreview.setImageDrawable(guideImageValue);
    }


    public void hidePreview() {
        ImageLoader.getInstance().stop();
        mPbLoading.setVisibility(View.GONE);
    }


}
